Dr.Vikas Thada's Blogs

Know How Leap Ahead

Menu

Function as Parameter in Python

A function can also be passed as an argument in function. The reason behind that a function can easily be given a new name and when passed into another function during function call , it can be collected into function formal parameters. It is because functions are first class objects in python just like integer, float, list, strings etc.

Passing Variable Arguments to Functions in Python

It comes handy at times when you have the facility to pass variable number of arguments to function. Python has a special feature which allow us to pass variable number of arguments to functions. That feature is known as passing parameters with asterisk. A python function having single parameter with asterisk (*) as prefix can accept variable number of arguments. The parameter name serve as a tuple with variable number and types of arguments. The arguments cannot be keyword arguments as we will see in a short while.

The Keyword Arguments (**kwargs) in Python

In my previous blog I explained the concept of passing variable number of arguments using * operator in python. Apart from passing variable number of non-keyword argument using *args syntax, you can also pass variable number of keyword arguments using **kwargs syntax. Every keyword has a value. The name of word is treated as keys of dictionary and name value is equivalent to corresponding value of the key.

Global Variables in Python Functions

All variables that are defined within functions are local variables. These variables have their scope limited to functions in which they were defined. They cannot be used outside them. The variables which are defined in default main function (not visible) are global and they can be used in any function. The keyword global is useful in these situations. In this article we are going to understand the usage of keyword global in python and how it can come to your rescue in some programming situations. Let us understand this using some examples: